<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/staff/om/Staff.php");



/**
 * BaseStaffForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseStaffForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * staff
     *
     * @var Staff 
     */
    protected $staff;


    /**
     * password2
     *
     * @var mixed 
     */
    protected $password2;


    /**
     * roles
     *
     * @var mixed 
     */
    protected $roles;


    /**
     * formImage
     *
     * @var mixed 
     */
    protected $formImage;


    /**
     * deleteImage
     *
     * @var mixed 
     */
    protected $deleteImage;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseStaffForm
     */
    public function __construct() {
        $this->staff = new Staff();
        $this->password2 = null;
        $this->roles = array();
        $this->formImage = null;
        $this->deleteImage = false;
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get Staff
     *
     * @return Staff 
     */
    public function getStaff() {
        return $this->staff;
    }


    /**
     * Set Staff
     *
     * @param Staff  staff
     */
    public function setStaff(Staff $staff) {
        $this->staff = $staff;
    }


    /**
     * Get Password2
     *
     * @return mixed 
     */
    public function getPassword2() {
        return $this->password2;
    }


    /**
     * Set Password2
     *
     * @param mixed  password2
     */
    public function setPassword2($password2) {
        $this->password2 = $password2;
    }


    /**
     * Get Roles
     *
     * @return mixed 
     */
    public function getRoles() {
        return $this->roles;
    }


    /**
     * Set Roles
     *
     * @param mixed  roles
     */
    public function setRoles($roles) {
        $this->roles = $roles;
    }


    /**
     * Get FormImage
     *
     * @return mixed 
     */
    public function getFormImage() {
        return $this->formImage;
    }


    /**
     * Set FormImage
     *
     * @param mixed  formImage
     */
    public function setFormImage($formImage) {
        $this->formImage = $formImage;
    }


    /**
     * Get DeleteImage
     *
     * @return mixed 
     */
    public function getDeleteImage() {
        return $this->deleteImage;
    }


    /**
     * Set DeleteImage
     *
     * @param mixed  deleteImage
     */
    public function setDeleteImage($deleteImage) {
        $this->deleteImage = $deleteImage;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getStaff()->getFirstname()) || strlen($this->getStaff()->getFirstname()) == 0)
            $errors->add("staff->firstname",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.firstname", true)));


        if(!is_null($this->getStaff()->getFirstname()) && strlen($this->getStaff()->getFirstname()) > 0 && strlen($this->getStaff()->getFirstname()) > 255)
            $errors->add("staff->firstname",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.firstname", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getStaff()->getSurname()) || strlen($this->getStaff()->getSurname()) == 0)
            $errors->add("staff->surname",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.surname", true)));


        if(!is_null($this->getStaff()->getSurname()) && strlen($this->getStaff()->getSurname()) > 0 && strlen($this->getStaff()->getSurname()) > 255)
            $errors->add("staff->surname",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.surname", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getStaff()->getUsername()) || strlen($this->getStaff()->getUsername()) == 0)
            $errors->add("staff->username",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.username", true)));


        if(!is_null($this->getStaff()->getUsername()) && strlen($this->getStaff()->getUsername()) > 0 && strlen($this->getStaff()->getUsername()) > 255)
            $errors->add("staff->username",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.username", true),
                    new ActionMessage("255", false))));


        if(!is_null($this->getStaff()->getUsername()) && strlen($this->getStaff()->getUsername()) > 0 && !ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $this->getStaff()->getUsername()))
            $errors->add("staff->username",
                new ActionMessage("errors.email", true, new ActionMessage("prompt.username", true)));


        if(!is_null($this->getStaff()->getPassword()) && strlen($this->getStaff()->getPassword()) > 0 && strlen($this->getStaff()->getPassword()) < 5)
            $errors->add("staff->password",
                new ActionMessage("errors.minlength", true, array(
                    new ActionMessage("prompt.password", true),
                    new ActionMessage("5", false))));

        if(!is_null($this->getStaff()->getPassword()) && strlen($this->getStaff()->getPassword()) > 0 && strlen($this->getStaff()->getPassword()) > 255)
            $errors->add("staff->password",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.password", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getStaff()->getLanguageCode()) || strlen($this->getStaff()->getLanguageCode()) == 0)
            $errors->add("staff->languageCode",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.language", true)));


        if(!is_null($this->getStaff()->getLanguageCode()) && strlen($this->getStaff()->getLanguageCode()) > 0 && strlen($this->getStaff()->getLanguageCode()) > 255)
            $errors->add("staff->languageCode",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.language", true),
                    new ActionMessage("255", false))));


        global $ALLOWABLE_IMAGES;
        if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
            $errors->add("formImage",
                new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

        return $errors;
    }

}
?>